package com.yuan.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yuan.pojo.AxdTestAppUser;
import com.yuan.pojo.IRData;
import com.yuan.pojo.dto.IRDataDto;
import com.yuan.service.AxdTestAppUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/*
 *  @author 雨安
 *  类名： JsonController
 *  创建时间：2024/3/17
 */
@RestController
@CrossOrigin //跨域处理
public class JsonController {
    @Autowired
    AxdTestAppUserService axdAppUserService;

    @PostMapping(value ="/getJson",produces = "application/json; charset=UTF-8")
        public String haha(@RequestBody String str) throws JsonProcessingException {
        //遍历
        ObjectMapper toObj = new ObjectMapper();
        //得到转换对象
        IRDataDto ir = toObj.readValue(str, IRDataDto.class);
        //拿到数据对象
        List<IRData> data = ir.getData();
        //创建接收集合
        List<AxdTestAppUser> axdList = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            //判断当前数据不为null
            if (data.get(i)!=null && data.get(i).toString().trim().length()!=0){
                //创建接收数据对象
                AxdTestAppUser axd = new AxdTestAppUser();
                //给username字段赋默认值
                //非空判断
                if (StringUtils.isNotBlank(data.get(i).getEmpidnum())){
                    //身份证号
                    axd.setIdNumber(data.get(i).getEmpidnum());
                }
                //非空判断
                if (StringUtils.isNotBlank(data.get(i).getEmpmobile())) {
                    //手机号码
                    axd.setPhoneNumber(data.get(i).getEmpmobile());
                }
                //非空判断
                if (StringUtils.isNotBlank(data.get(i).getEmpsexname())){
                    //性别
                    if (data.get(i).getEmpsexname().equals("男")){
                        axd.setSex('0');
                    }else if (data.get(i).getEmpsexname().equals("女")){
                        axd.setSex('1');
                    }else {
                        axd.setSex('2');
                    }
                }
                //非空判断
                if (StringUtils.isNotBlank(data.get(i).getEmpname())){
                    //真实姓名
                    axd.setRealName(data.get(i).getEmpname());
                    //设置username
                    axd.setUserName(data.get(i).getEmpname());
                }
                //添加到集合中
                axdList.add(axd);
            }
        }
        //添加到数据库中
        boolean b = axdAppUserService.saveBatch(axdList);
        return b==true?"成功":"失败";
    }
}
